﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataAccessLibrary.DAL;
using entities;

namespace BusinessLogicLibrary
{
    public class SupplyModule
    {
        private readonly CheckinDAO _checkinDAO = new CheckinDAO();
        private readonly SupplyDAO _supplyDAO = new SupplyDAO();

        public bool AddSupplyDetail(bool available, string supplyName, decimal price, string unit)
        {
            if (supplyName.Trim() == "" || unit == "")
            {
                throw new ArgumentNullException();
            }
            var supplyDetail = new SupplyDetail
                                   {
                                       Available = available,
                                       SupplyName = supplyName,
                                       SupplyPrice = price,
                                       Unit = unit
                                   };
            return _supplyDAO.InsertSupplyDetail(supplyDetail);
        }

        public List<SupplyDetail> GetSupplyDetails(bool available)
        {
            return available ? _supplyDAO.SelectSupplyDetails(1) : _supplyDAO.SelectSupplyDetails(-1);
        }

        public SupplyDetail GetSupplyDetail(int supplyDetailId)
        {
            return _supplyDAO.SelectSupplyDetail(supplyDetailId);
        }

        public List<SupplyDetail> GetSupplyDetails(string supplyDetailName, bool available)
        {
            return available
                       ? _supplyDAO.SelectSupplyDetails(supplyDetailName, 1)
                       : _supplyDAO.SelectSupplyDetails(supplyDetailName, -1);
        }

        public bool ModifySupplyDetail(int id, bool available, string supplyName, decimal price, string unit)
        {
            if (supplyName.Trim() == "" || unit == "")
            {
                throw new ArgumentNullException();
            }
            var supplyDetail = new SupplyDetail
                                   {
                                       SupplyDetailID = id,
                                       Available = available,
                                       SupplyName = supplyName,
                                       SupplyPrice = price,
                                       Unit = unit
                                   };
            return _supplyDAO.UpdateSupplyDetail(supplyDetail);
        }

        public bool AddSupplies(List<int> supplyDetailIds, int checkinId)
        {
            CheckIn checkIn = _checkinDAO.SelectCheckIn(checkinId);
            if (checkIn.CheckInEmployee < 1) throw new Exception("Invalid Checkin");
            IEnumerable<IGrouping<int, int>> gEnumerable = supplyDetailIds.GroupBy(p => p);
            List<Supply> supplies =
                gEnumerable.Select(
                    en =>
                    new Supply
                        {
                            SupplyDetailID = en.First(),
                            SupplyNumber = en.Key,
                            CheckInID = checkIn.CheckInID,
                            SupplyDate = DateTime.Now
                        }).ToList();
            return _supplyDAO.InsertSupplies(supplies);
        }

        public List<Supply> GetSupplies(int checkinId)
        {
            return _supplyDAO.SelectSupplies(checkinId);
        }
    }
}